<?php

namespace app\apiadmin\service\action;

use app\apiadmin\controller\live\UnionApp;
use app\apiadmin\controller\user\User;
use app\apiadmin\model\action\ActionModel;
use app\apiadmin\model\choujiang\ChouJiangModel;
use app\apiadmin\model\choujiang\PrizeUserModel;
use app\apiadmin\model\choujiang\ShuZiRenModel;
use app\apiadmin\model\system\UserModel;
use app\apiadmin\service\data\CommStaticDictService;
use app\apiadmin\service\data\RedisService;
use app\apiadmin\service\statics\MongoDbService;
use app\common\utils\Res;
use DateTime;
use DateTimeZone;
use MongoDB\BSON\ObjectId;
use think\db\connector\Mongo;
use think\db\Mongo as DbMongo;
use MongoDB\BSON\Regex;
use think\facade\Db;


use app\apiadmin\service\TCSS\TImService;
use MongoDate;
use MongoDB\BSON\UTCDateTime;
use think\facade\Log;

class ActionService
{
    public static function getZhuJiaoInfo()
    {

        $result = ActionModel::getZhuJiaoInfo();

        return Res::ok($result);

    }

    public static function getYaoqingZhujiao2($param)
    {

        $map = [];
        $map['del'] = 0;
        $map['live_id'] = $param['liveId'];
        $collection = 'live_invitation';
        $result = ActionModel::getYaoqingZhujiao2($collection, $map);

        return res::ok($result);
    }




    public static function getUserInfo($param)
    {

        $limit = !isset($param['pageSize']) ? 10 : (int) $param['pageSize'];
        $offset = ($param['pageNumber'] - 1) * $limit;

        $map = [];
        $map['del'] = "0";
        $liveId = $param['liveId'];
        if (isset($param["input"]) && !empty($param["input"])) {
            if (isset($param["typeinput"]) && !empty($param["typeinput"])) {
                switch ($param["typeinput"]) {
                    case 1:  //用户昵称
                        // 构建查询
                        $map['nickname'] = ['$regex' => $param['input'], '$options' => 'i'];
                        break;
                    case 2:  //用户手机号
                        // 构建查询
                        $map['phone'] = ['$regex' => $param['input'], '$options' => 'i'];
                        break;
                    default:
                        # code...
                        break;
                }
            }
        }

        if (isset($param["online"]) && $param["online"] != "") {
            $map['online_status'] = '' . $param["online"];
        }

        $zhujiao = '';
        if (isset($param["zhujiao"]) && !empty($param["zhujiao"])) {
            $zhujiao = $param["zhujiao"];
        }

        if (isset($param["live_user_id"]) && !empty($param["live_user_id"])) {
            $map['live_user_id'] = $param["live_user_id"];
        }

        //查询用户信息
        $findName = [];
        $collection = 'live_user_' . $liveId;
        $result = ActionModel::pageMap_userinfo($collection, $map, $findName, $offset, $limit);
        $total = ActionModel::pageMapCount_userinfo($collection, $map);

        //查询邀请助教
        $userids = array_column($result, "live_user_id");
        $map4 = [];
        $map4["del"] = 0;
        $map4["liveId"] = $param['liveId'];
        $map4['user_id'] = ['$in' => $userids];
        if (isset($param['weizhifu']) && $param['weizhifu'] == 1) {
            $map4['event'] = '提单未付款';
        }
        $collection = 'live_event';

        $result4 = ActionModel::pageMap_shijian($collection, $map4, $findName, $offset, $limit);

        $map2 = [];
        $map2['del'] = 0;
        $map2['live_id'] = $liveId;
        $map2['user_id'] = ['$in' => $userids];
        $zhujiao2 = '';
        if (isset($param["zhujiao2"]) && !empty($param["zhujiao2"])) {
            $map2['tutor_id'] = '' . $param["zhujiao2"];
            $zhujiao2 = $param["zhujiao2"];
        }
        if (isset($param["tutorId"]) && !empty($param["tutorId"])) {
            $map2['tutor_id'] = $param["tutorId"];
            $zhujiao2 = $param["tutorId"];
        }


        $collection = 'live_invitation';
        $result2 = ActionModel::getYaoqingZhujiao($collection, $map2);

        //查询接量助教
        $unionids = array_column($result, "wx_unionid");

        $where = array();
        $where[] = array('a.union_id', 'in', $unionids);

        $findName = 'a.union_id,b.name,a.teacher_id';

        $result3 = ActionModel::getJieliangZhujiao($where, $findName);

        $data2 = [];
        foreach ($result as $k => $v) {
            if ($zhujiao) {
                if (isset($v['online_time']) && !empty($v['online_time'])) {
                    $result[$k]['zaixian_shichang'] = gmdate("H:i:s", $v['online_time']);   //在线时长
                } else {
                    $result[$k]['zaixian_shichang'] = 0;
                }

                $result[$k]['yaoqingzhujiao'] = '-';  //邀请助教
                foreach ($result2 as $k2 => $v2) {
                    if ($v['live_user_id'] == $v2['user_id']) {
                        if ($v2['tutor_nickname']) {
                            $result[$k]['yaoqingzhujiao'] = $v2['tutor_nickname'];
                        } else {
                            $result[$k]['yaoqingzhujiao'] = '-';
                        }

                        $result[$k]['tutor_id'] = $v2['tutor_id'];
                    }
                }

                $a = false;
                $result[$k]['jieliangzhujiao'] = '-';   //接量助教
                foreach ($result3 as $k3 => $v3) {
                    if (isset($v['wx_unionid']) && $v['wx_unionid'] == $v3['union_id']) {
                        $result[$k]['jieliangzhujiao'] = $v3['name'];
                        $result[$k]['jieliang_userid'] = $v3['teacher_id'];
                    }
                    if ($v3['teacher_id'] == $zhujiao) {
                        $a = true;
                    }
                }

                $result[$k]['typename'] = '-';   //点击详情
                $result[$k]['shijian'] = '-';   //提单未付款
                $result[$k]['payorder'] = '-';  //购买记录
                foreach ($result4 as $k4 => $v4) {
                    if ($v4['user_id'] == $v['live_user_id']) {
                        if ($v4['type'] == 1) {
                            $result[$k]['typename'] = '小黄车内点击-' . date('H:i:s', $v4['create_time']);
                        } else if ($v4['type'] == 2) {
                            $result[$k]['typename'] = '右下角课程点击-' . date('H:i:s', $v4['create_time']);
                        } else if ($v4['type'] == 20) {
                            $result[$k]['typename'] = '商品弹窗点击-' . date('H:i:s', $v4['create_time']);
                        }

                        if ($v4['event'] == '提单未付款') {
                            $result[$k]['shijian'] = $v4['coursename'] . '-' . date('H:i:s', $v4['create_time']);
                        }
                        if ($v4['event'] == '下单') {
                            $result[$k]['payorder'] = $v4['coursename'] . '-' . date('H:i:s', $v4['create_time']);
                        }
                    }
                }
                if ($a) {
                    $data2[] = $result[$k];
                }

            } else {

                if (isset($v['online_time']) && !empty($v['online_time'])) {
                    $result[$k]['zaixian_shichang'] = gmdate("H:i:s", $v['online_time']);
                } else {
                    $result[$k]['zaixian_shichang'] =  0;
                }
                $result[$k]['yaoqingzhujiao'] = '-';
                $result[$k]['tutor_id'] = '-';
                foreach ($result2 as $k2 => $v2) {
                    if ($v['live_user_id'] == $v2['user_id']) {
                        $result[$k]['yaoqingzhujiao'] = $v2['tutor_nickname'];
                        $result[$k]['tutor_id'] = $v2['tutor_id'];
                    }
                }

                $result[$k]['jieliangzhujiao'] = '-';
                foreach ($result3 as $k3 => $v3) {
                    if (isset($v['wx_unionid']) && ($v['wx_unionid'] == $v3['union_id'])) {
                        $result[$k]['jieliangzhujiao'] = $v3['name'];
                    }
                }

                $result[$k]['typename'] = '-';
                $result[$k]['shijian'] = '-';
                $result[$k]['payorder'] = '-';
                foreach ($result4 as $k4 => $v4) {
                    if ($v4['user_id'] == $v['live_user_id']) {
                        if ($v4['type'] == 1) {
                            $result[$k]['typename'] = '小黄车内点击-' . date('H:i:s', $v4['create_time']);
                        } else if ($v4['type'] == 2) {
                            $result[$k]['typename'] = '右下角课程点击-' . date('H:i:s', $v4['create_time']);
                        } else if ($v4['type'] == 20) {
                            $result[$k]['typename'] = '商品弹窗点击-' . date('H:i:s', $v4['create_time']);
                        }

                        if ($v4['event'] == '提单未付款') {
                            $result[$k]['shijian'] = $v4['coursename'] . '-' . date('H:i:s', $v4['create_time']);
                        }
                        if ($v4['event'] == '下单') {
                            $result[$k]['payorder'] = $v4['coursename'] . '-' . date('H:i:s', $v4['create_time']);
                        }
                    }
                }
                $data2[] = $result[$k];
            }

        }

        if ($zhujiao2) {
            $data3 = [];
            foreach ($data2 as $k => $v) {
                if ($v['tutor_id'] == $zhujiao2) {
                    $data3[] = $v;
                }
            }
            $data = [];
            $data2 = $data3;
        }
        
        $data = [
            'list' => $data2,
            'total' => $total,
            'result' => $result,
            'result2' => $result2,
            'result3' => $result3,
            'result4' => $result4,
            'map' => $map
        ];
        return Res::ok($data);

    }

    public static function getUserInfoDc($param)
    {

        $limit = !isset($param['pageSize']) ? 10 : (int) $param['pageSize'];
        $offset = ($param['pageNumber'] - 1) * $limit;

        $map = [];
        $map['del'] = "0";
        $liveId = $param['liveId'];
        if (isset($param["input"]) && !empty($param["input"])) {
            if (isset($param["typeinput"]) && !empty($param["typeinput"])) {
                switch ($param["typeinput"]) {
                    case 1:  //用户昵称
                        // 构建查询
                        $map['nickname'] = ['$regex' => $param['input'], '$options' => 'i'];
                        break;
                    case 2:  //用户手机号
                        // 构建查询
                        $map['phone'] = ['$regex' => $param['input'], '$options' => 'i'];
                        break;
                    default:
                        # code...
                        break;
                }
            }
        }

        if (isset($param["online"]) && $param["online"] != "") {
            $map['online_status'] = '' . $param["online"];
        }

        $zhujiao = '';
        if (isset($param["zhujiao"]) && !empty($param["zhujiao"])) {
            $zhujiao = $param["zhujiao"];
        }

        if (isset($param["live_user_id"]) && !empty($param["live_user_id"])) {
            $map['live_user_id'] = $param["live_user_id"];
        }

        //查询用户信息
        $findName = [];
        $collection = 'live_user_' . $liveId;
        $result = ActionModel::pageMap_userinfo($collection, $map, $findName, $offset, $limit);

        //查询邀请助教
        $userids = array_column($result, "live_user_id");
        $map4 = [];
        $map4["del"] = 0;
        $map4["liveId"] = $param['liveId'];
        $map4['user_id'] = ['$in' => $userids];
        if (isset($param['weizhifu']) && $param['weizhifu'] == 1) {
            $map4['event'] = '提单未付款';
        }
        $collection = 'live_event';

        $result4 = ActionModel::pageMap_shijian($collection, $map4, $findName, $offset, $limit);

        $map2 = [];
        $map2['del'] = 0;
        $map2['live_id'] = $liveId;
        $map2['user_id'] = ['$in' => $userids];
        $zhujiao2 = '';
        if (isset($param["zhujiao2"]) && !empty($param["zhujiao2"])) {
            $map2['tutor_id'] = '' . $param["zhujiao2"];
            $zhujiao2 = $param["zhujiao2"];
        }
        if (isset($param["tutorId"]) && !empty($param["tutorId"])) {
            $map2['tutor_id'] = $param["tutorId"];
            $zhujiao2 = $param["tutorId"];
        }


        $collection = 'live_invitation';
        $result2 = ActionModel::getYaoqingZhujiao($collection, $map2);

        //查询接量助教
        $unionids = array_column($result, "wx_unionid");

        $where = array();
        $where[] = array('a.union_id', 'in', $unionids);

        $findName = 'a.union_id,b.name,a.teacher_id';

        $result3 = ActionModel::getJieliangZhujiao($where, $findName);

        $data2 = [];
        foreach ($result as $k => $v) {
            if ($zhujiao) {
                if (isset($v['online_time']) && !empty($v['online_time'])) {
                    $result[$k]['zaixian_shichang'] = gmdate("H:i:s", $v['online_time']);   //在线时长
                } else {
                    $result[$k]['zaixian_shichang'] = 0;
                }

                $result[$k]['yaoqingzhujiao'] = '-';  //邀请助教
                foreach ($result2 as $k2 => $v2) {
                    if ($v['live_user_id'] == $v2['user_id']) {
                        if ($v2['tutor_nickname']) {
                            $result[$k]['yaoqingzhujiao'] = $v2['tutor_nickname'];
                        } else {
                            $result[$k]['yaoqingzhujiao'] = '-';
                        }

                        $result[$k]['tutor_id'] = $v2['tutor_id'];
                    }
                }

                $a = false;
                $result[$k]['jieliangzhujiao'] = '-';   //接量助教
                foreach ($result3 as $k3 => $v3) {
                    if (isset($v['wx_unionid']) && $v['wx_unionid'] == $v3['union_id']) {
                        $result[$k]['jieliangzhujiao'] = $v3['name'];
                        $result[$k]['jieliang_userid'] = $v3['teacher_id'];
                    }
                    if ($v3['teacher_id'] == $zhujiao) {
                        $a = true;
                    }
                }

                $result[$k]['typename'] = '-';   //点击详情
                $result[$k]['shijian'] = '-';   //提单未付款
                $result[$k]['payorder'] = '-';  //购买记录
                foreach ($result4 as $k4 => $v4) {
                    if ($v4['user_id'] == $v['live_user_id']) {
                        if ($v4['type'] == 1) {
                            $result[$k]['typename'] = '小黄车内点击-' . date('H:i:s', $v4['create_time']);
                        } else if ($v4['type'] == 2) {
                            $result[$k]['typename'] = '右下角课程点击-' . date('H:i:s', $v4['create_time']);
                        } else if ($v4['type'] == 20) {
                            $result[$k]['typename'] = '商品弹窗点击-' . date('H:i:s', $v4['create_time']);
                        }

                        if ($v4['event'] == '提单未付款') {
                            $result[$k]['shijian'] = $v4['coursename'] . '-' . date('H:i:s', $v4['create_time']);
                        }
                        if ($v4['event'] == '下单') {
                            $result[$k]['payorder'] = $v4['coursename'] . '-' . date('H:i:s', $v4['create_time']);
                        }
                    }
                }
                if ($a) {
                    $data2[] = $result[$k];
                }

            } else {

                if (isset($v['online_time']) && !empty($v['online_time'])) {
                    $result[$k]['zaixian_shichang'] = gmdate("H:i:s", $v['online_time']);
                } else {
                    $result[$k]['zaixian_shichang'] =  0;
                }
                $result[$k]['yaoqingzhujiao'] = '-';
                $result[$k]['tutor_id'] = '-';
                foreach ($result2 as $k2 => $v2) {
                    if ($v['live_user_id'] == $v2['user_id']) {
                        $result[$k]['yaoqingzhujiao'] = $v2['tutor_nickname'];
                        $result[$k]['tutor_id'] = $v2['tutor_id'];
                    }
                }

                $result[$k]['jieliangzhujiao'] = '-';
                foreach ($result3 as $k3 => $v3) {
                    if (isset($v['wx_unionid']) && ($v['wx_unionid'] == $v3['union_id'])) {
                        $result[$k]['jieliangzhujiao'] = $v3['name'];
                    }
                }

                $result[$k]['typename'] = '-';
                $result[$k]['shijian'] = '-';
                $result[$k]['payorder'] = '-';
                foreach ($result4 as $k4 => $v4) {
                    if ($v4['user_id'] == $v['live_user_id']) {
                        if ($v4['type'] == 1) {
                            $result[$k]['typename'] = '小黄车内点击-' . date('H:i:s', $v4['create_time']);
                        } else if ($v4['type'] == 2) {
                            $result[$k]['typename'] = '右下角课程点击-' . date('H:i:s', $v4['create_time']);
                        } else if ($v4['type'] == 20) {
                            $result[$k]['typename'] = '商品弹窗点击-' . date('H:i:s', $v4['create_time']);
                        }

                        if ($v4['event'] == '提单未付款') {
                            $result[$k]['shijian'] = $v4['coursename'] . '-' . date('H:i:s', $v4['create_time']);
                        }
                        if ($v4['event'] == '下单') {
                            $result[$k]['payorder'] = $v4['coursename'] . '-' . date('H:i:s', $v4['create_time']);
                        }
                    }
                }
                $data2[] = $result[$k];
            }

        }

        if ($zhujiao2) {
            $data3 = [];
            foreach ($data2 as $k => $v) {
                if ($v['tutor_id'] == $zhujiao2) {
                    $data3[] = $v;
                }
            }
            $data = [];
            $data2 = $data3;
        }
//        foreach ($data2 as $k => $v) {
//            //事件
//            $param = array();
//            $param['input'] = '';
//            $param['type'] = '';
//            $param['liveId'] = $liveId;
//            $param['user_id'] = $v['live_user_id'];
//            $param['pageSize'] = 9999;
//            $param['pageNumber'] = 1;
//            $result1 = ActionService::getShijian($param);
//            $result2 = $result1->getData();
//            $shijian1 = $result2['data']['list'];
//            $sc = array();
//            foreach ($shijian1 as $k1 => $v1) {
//                $sc[] = $v1['typename'].$v1['dianji_time'];
//            }
//            $shijian2 = join('/br',$sc);
//            $data2[$k]['djxq'] = $v['shijian'].$shijian2;
//
//            //提单未付款
//            $param1 = array();
//            $param1['input'] = '';
//            $param1['state'] = '';
//            $param1['liveId'] = $liveId;
//            $param1['user_id'] = $v['live_user_id'];
//            $param1['pageSize'] = 9999;
//            $param1['pageNumber'] = 1;
//            $param1['fliter'] = 1;
//            $result3 = ActionService::getpayorder($param1);
//            $result4 = $result3->getData();
//            $weifukuan = $result4['data']['list'];
//            $wfk = array();
//            foreach ($weifukuan as $k2 => $v2) {
//                $wfk[] = $v2['coursename'].$v2['pay_time'];
//            }
//            $wfk1 = join('/br',$wfk);
//            $data2[$k]['tdwfk'] = $v['shijian'].$wfk1;
//
//            //课程购买记录
//            $data2[$k]['kcgmjl'] = $v['payorder'].$wfk1;
//        }
        $data = [
            'list' => $data2,
        ];
        return Res::ok($data);

    }

    public static function delUserInfo($param)
    {

        $map = [];
        $liveId = $param['liveId'];

        $collection = 'live_user_' . $liveId;

        $map['_id'] = new ObjectID($param['id']['$oid']);
        $update = ['$set' => ['del' => 1]]; // 定义更新操作
        $data = ActionModel::delUserInfo($collection, $map, $update);

        if ($data) {
            return Res::ok('删除成功');
        } else {
            return Res::error('删除失败');
        }

    }


    public static function pldelUserInfo($param)
    {
        $map = [];
        $liveId = $param['liveId'];

        $ids = [];
        foreach ($param['ids'] as $k => $v) {
            $ids[] = new ObjectID($v['$oid']);
        }

        $collection = 'live_user_' . $liveId;

        $map['_id'] = ['$in' => $ids];
        $update = ['$set' => ['del' => 1]]; // 定义更新操作
        $data = ActionModel::pldelUserInfo($collection, $map, $update);

        if ($data) {
            return Res::ok('删除成功');
        } else {
            return Res::error('删除失败');
        }
    }

    public static function getuser($param)
    {
        $map = [];
        $map['live_user_id'] = $param['live_user_id'];
        $liveId = $param['liveId'];

        $collection = 'live_user_' . $liveId;
        $data = ActionModel::getuser($collection, $map);
        return Res::ok($data);
    }

    // /**
    //  * @author: zwx
    //  * @Description: 邀约到课  明细
    //  * @param {*} $param
    //  * @Date: 2024-07-04 00:58:45
    //  */    
    // public static function getyaoqing($param)
    // {
    //     $limit = !isset($param['pageSize']) ? 10 : (int) $param['pageSize'];
    //     $offset = ($param['pageNumber'] - 1) * $limit;

    //     $map = [];
    //     $map['del'] = 0;
    //     $map['live_id'] = $param['liveId'];
    //     // $map['status'] = 2;

    //     if (isset($param["tutor_id"]) && !empty($param["tutor_id"])) {
    //         $map['tutor_id'] = $param["tutor_id"];
    //     }

    //     if (isset($param["user_id"]) && !empty($param["user_id"])) {
    //         $map['user_id'] = $param["user_id"];
    //     }

    //     if (isset($param["input"]) && !empty($param["input"])) {
    //         if (isset($param["typeinput"]) && !empty($param["typeinput"])) {
    //             switch ($param["typeinput"]) {
    //                 case 1:  //邀请助教名字
    //                     // 构建查询
    //                     $map['tutor_nickname'] = ['$regex' => $param['input'], '$options' => 'i'];
    //                     break;
    //                 case 2:  //被邀请用户名字
    //                     // 构建查询
    //                     $map['nickname'] = ['$regex' => $param['input'], '$options' => 'i'];
    //                     break;
    //                 case 3:  //被邀请用户名字
    //                     // 构建查询
    //                     $map['phone'] = ['$regex' => $param['input'], '$options' => 'i'];
    //                     break;
    //                 default:
    //                     # code...
    //                     break;
    //             }
    //         }
    //     }


    //     //查询用户信息
    //     $findName = [];
    //     $collection = 'live_invitation';
    //     $result = ActionModel::pageMap_yaoqing($collection, $map, $findName, $offset, $limit);
    //     $total = ActionModel::pageMapCount_yaoqing($collection, $map);

    //     foreach ($result as $k => $v) {
    //         $result[$k]['login_time'] = '-';
    //         $result[$k]['from'] = '-';
    //         if (!empty($v['login_time']) && ($v['status'] == 2)) {
    //             $result[$k]['login_time'] = date('Y-m-d H:i:s', $v['booking_time']);
    //         }
    //     }


    //     $data = [
    //         'list' => $result,
    //         'total' => $total,
    //     ];
    //     return Res::ok($data);
    // }


    /**
     * @author: zwx
     * @Description: 邀约到课  明细
     * @param {*} $param
     * @Date: 2024-07-04 00:58:45
     */
    public static function getyaoqing($param)
    {
        $limit = !isset($param['pageSize']) ? 10 : (int) $param['pageSize'];
        $offset = ($param['pageNumber'] - 1) * $limit;

        $map = [];
        $map['xh_siyu_invitation_user.del'] = 0;
        $map['xh_siyu_invitation_user.liveId'] = $param['liveId'];

        if (isset($param['sender']) && !empty($param["sender"])) {
            $map['xh_siyu_invitation_user.sender'] = $param['sender'];
        }





        $kehu = db::table('xh_siyu_invitation_user')
            // ->field('xh_siyu_invitation_user.id,xh_siyu_invitation_user.sender,xh_siyu_invitation_user.signin_time as login_time,xh_siyu_invitation_user.signin_status as status')
            // ->join('xh_siyu_user', 'xh_siyu_user.unionid = xh_siyu_invitation_user.wx_unionid', 'left') // 注意这里
            ->where($map)
            ->limit($offset, $limit)
            ->select()->toArray();

        $total = db::table('xh_siyu_invitation_user')
            // ->field('xh_siyu_invitation_user.signin_time as login_time,xh_siyu_invitation_user.signin_status as status')
            // ->join('xh_siyu_user', 'xh_siyu_user.unionid = xh_siyu_invitation_user.wx_unionid', 'left') // 注意这里
            ->where($map)->count();

        $sender = array_column($kehu, 'sender');
        $senders = db::table('xh_siyu_sys_member')->where('user_name', 'in', $sender)->select()->toArray();

        foreach ($kehu as $k => $v) {
            $kehu[$k]['id'] = $offset + $k + 1;
            $kehu[$k]['tutor_nickname'] = '-';
            foreach ($senders as $k2 => $v2) {
                if ($v['sender'] == $v2['user_name']) {
                    $kehu[$k]['tutor_nickname'] = $v2['nick_name'];
                }
            }

        }

        $data = [
            'list' => $kehu,
            'total' => $total,
        ];
        return Res::ok($data);
    }



    /**
     * @author: zwx
     * @Description: 邀请总览  明细
     * @param {*} $param
     * @Date: 2024-07-04 00:58:45
     */
    public static function getyaoqingzonglan($param)
    {
        $limit = !isset($param['pageSize']) ? 10 : (int) $param['pageSize'];
        $offset = ($param['pageNumber'] - 1) * $limit;

        $map = [];
        $map['xh_siyu_invitation_user.del'] = 0;
        $map['xh_siyu_invitation_user.liveId'] = $param['liveId'];

        if (isset($param['sender']) && !empty($param["sender"])) {
            $map['xh_siyu_invitation_user.sender'] = $param['sender'];
        }

        if (isset($param["input"]) && !empty($param["input"])) {
            if (isset($param["typeinput"]) && !empty($param["typeinput"])) {
                switch ($param["typeinput"]) {
                    case 1:  //邀请助教名字
                        // 构建查询
                        $map[] = array('xh_siyu_sys_member.nick_name', 'like', '%' . $param['input'] . '%');
                        break;
                    case 2:  //被邀请用户名字
                        // 构建查询
                        $map[] = array('xh_siyu_invitation_user.nickname', 'like', '%' . $param['input'] . '%');
                        break;
                    case 3:  //被邀请用户手机号
                        // 构建查询
                        $map['xh_siyu_invitation_user.phone'] = $param['input'];
                        break;
                    default:
                        # code...
                        break;
                }
            }
        }



        $kehu = db::table('xh_siyu_invitation_user')
            // ->field('xh_siyu_invitation_user.id,xh_siyu_invitation_user.sender,xh_siyu_invitation_user.signin_time as login_time,xh_siyu_invitation_user.signin_status as status')
            // ->join('xh_siyu_user', 'xh_siyu_user.unionid = xh_siyu_invitation_user.wx_unionid', 'left') // 注意这里
            ->join('xh_siyu_sys_member', 'xh_siyu_sys_member.user_name = xh_siyu_invitation_user.sender', 'left') // 注意这里
            ->where($map)
            ->limit($offset, $limit)
            ->select()->toArray();

        $msg = db::table('xh_siyu_invitation_user')
            // ->field('xh_siyu_invitation_user.id,xh_siyu_invitation_user.sender,xh_siyu_invitation_user.signin_time as login_time,xh_siyu_invitation_user.signin_status as status')
            // ->join('xh_siyu_user', 'xh_siyu_user.unionid = xh_siyu_invitation_user.wx_unionid', 'left') // 注意这里
            ->join('xh_siyu_sys_member', 'xh_siyu_sys_member.user_name = xh_siyu_invitation_user.sender', 'left') // 注意这里
            ->where($map)
            ->limit($offset, $limit)
            ->fetchSql()
            ->select();

        $total = db::table('xh_siyu_invitation_user')
            // ->field('xh_siyu_invitation_user.signin_time as login_time,xh_siyu_invitation_user.signin_status as status')
            // ->join('xh_siyu_user', 'xh_siyu_user.unionid = xh_siyu_invitation_user.wx_unionid', 'left') // 注意这里
            ->join('xh_siyu_sys_member', 'xh_siyu_sys_member.user_name = xh_siyu_invitation_user.sender', 'left') // 注意这里
            ->where($map)->count();

        $sender = array_column($kehu, 'sender');
        $senders = db::table('xh_siyu_sys_member')->where('user_name', 'in', $sender)->select()->toArray();

        foreach ($kehu as $k => $v) {
            $kehu[$k]['id'] = $offset + $k + 1;
            $kehu[$k]['tutor_nickname'] = '-';
            foreach ($senders as $k2 => $v2) {
                if ($v['sender'] == $v2['user_name']) {
                    $kehu[$k]['tutor_nickname'] = $v2['nick_name'];
                }
            }

        }

        $data = [
            'list' => $kehu,
            'total' => $total,
            'msg' => $msg
        ];
        return Res::ok($data);
    }


    // public static function delyaoqing($param)
    // {
    //     $map = [];

    //     $collection = 'live_invitation';

    //     $map['_id'] = new ObjectID($param['id']['$oid']);
    //     $update = ['$set' => ['del' => 1]]; // 定义更新操作
    //     $data = ActionModel::delyaoqing($collection, $map, $update);

    //     if ($data) {
    //         return Res::ok('删除成功');
    //     } else {
    //         return Res::error('删除失败');
    //     }
    // }

    // public static function pldelyaoqing($param)
    // {
    //     $map = [];

    //     $ids = [];
    //     foreach ($param['ids'] as $k => $v) {
    //         $ids[] = new ObjectID($v['$oid']);
    //     }

    //     $collection = 'live_invitation';

    //     $map['_id'] = ['$in' => $ids];
    //     $update = ['$set' => ['del' => 1]]; // 定义更新操作
    //     $data = ActionModel::pldelyaoqing($collection, $map, $update);

    //     if ($data) {
    //         return Res::ok('删除成功');
    //     } else {
    //         return Res::error('删除失败');
    //     }
    // }


    public static function delyaoqing($param)
    {
        $map = [];
        $map['id'] = $param['id'];

        $data = [];
        $data['del'] = 1;

        $user = $data = db::table('xh_siyu_invitation_user')->where($map)->find();

        if ($user) {
            db::table('xh_siyu_invitation_user')->where($map)->save($data);
            return Res::ok('删除成功');
        } else {
            return Res::error('删除失败,未查询到的数据');
        }

    }

    public static function pldelyaoqing($param)
    {
        $map = [];
        $map[] = ['id', 'in', $param['ids']];

        $data = [];
        $data['del'] = 1;

        $users = $data = db::table('xh_siyu_invitation_user')->where($map)->select();

        if ($users) {
            db::table('xh_siyu_invitation_user')->where($map)->save($data);
            return Res::ok('删除成功');
        } else {
            return Res::error('删除失败,未查询到的数据');
        }


    }

    public static function getpinglun($param)
    {
        $limit = !isset($param['pageSize']) ? 10 : (int) $param['pageSize'];
        $offset = ($param['pageNumber'] - 1) * $limit;


        $map = [];
        $map['del'] = 0;
        $liveId = $param['liveId'];

        if (isset($param["input"]) && !empty($param["input"])) {
            if (isset($param["typeinput"]) && !empty($param["typeinput"])) {
                switch ($param["typeinput"]) {
                    case 1:  //用户昵称
                        // 构建查询
                        $map['nickname'] = ['$regex' => $param['input'], '$options' => 'i'];
                        break;
                    case 2:  //评论内容
                        // 构建查询
                        $map['content'] = ['$regex' => $param['input'], '$options' => 'i'];
                        break;
                    default:
                        # code...
                        break;
                }
            }
        }

        if ((isset($param["zhenshi_pingjia"]) && !empty($param["zhenshi_pingjia"])) || ($param["zhenshi_pingjia"] === 0)) {

            if ($param["zhenshi_pingjia"] == 1) {
                $map['is_real_msg'] = true;
            } else if ($param["zhenshi_pingjia"] == 2) {
                $map['is_real_msg'] = false;
            }
        }

        if ((isset($param["benchang_xinpinglun"]) && !empty($param["benchang_xinpinglun"])) || ($param["benchang_xinpinglun"] === 0)) {

            if ($param["benchang_xinpinglun"] == 1) {
                $map['is_new_msg'] = true;
            } else if ($param["benchang_xinpinglun"] == 2) {
                $map['is_new_msg'] = false;
            }
        }

        if (isset($param["live_user_id"])) {
            $map['live_user_id'] = $param['live_user_id'];
        }

        //查询用户信息
        $findName = [];
        $collection = 'live_user_msg_' . $liveId;
        $result = ActionModel::pageMap_pinglun($collection, $map, $findName, $offset, $limit);
        $total = ActionModel::pageMapCount_pinglun($collection, $map);

        //查询邀请助教
        $userids = array_column($result, "live_user_id");

        $map2 = [];
        $map2['del'] = 0;
        $map2['live_id'] = $liveId;
        $map2['user_id'] = ['$in' => $userids];

        $collection = 'live_invitation';
        $result2 = ActionModel::getYaoqingZhujiao($collection, $map2);


        foreach ($result as $k => $v) {
            if ($v['user_type'] == 'u') {
                $result[$k]['shenfen'] = '铁粉';
            } else if ($v['user_type'] == 'r') {
                $result[$k]['shenfen'] = '数字人';
            } else if ($v['user_type'] == 'a') {
                $result[$k]['shenfen'] = '场控';
            }
            // $result[$k]['send_time'] = date('Y-m-d H:i:s',$v['msg_send_time']) ;

            foreach ($result2 as $k2 => $v2) {
                $result[$k]['yaoqingzhujiao'] = '-';
                if ($v['live_user_id'] == $v2['user_id']) {
                    $result[$k]['yaoqingzhujiao'] = $v2['tutor_nickname'];
                }
            }
        }

        $data = [
            'list' => $result,
            'total' => $total,
        ];
        return Res::ok($data);
    }

    public static function pldelpinglun($param)
    {
        $map = [];

        $ids = [];
        foreach ($param['ids'] as $k => $v) {
            $ids[] = new ObjectID($v['$oid']);
        }

        $collection = 'live_user_msg_' . $param['liveId'];

        $map['_id'] = ['$in' => $ids];
        $update = ['$set' => ['del' => 1]]; // 定义更新操作
        $data = ActionModel::pldelpinglun($collection, $map, $update);

        if ($data) {
            return Res::ok('删除成功');
        } else {
            return Res::error('删除失败');
        }
    }

    public static function delpinglun($param)
    {
        $map = [];

        $collection = 'live_user_msg_' . $param['liveId'];

        $map['_id'] = new ObjectID($param['id']['$oid']);
        $update = ['$set' => ['del' => 1]]; // 定义更新操作
        $data = ActionModel::delpinglun($collection, $map, $update);

        if ($data) {
            return Res::ok('删除成功');
        } else {
            return Res::error('删除失败');
        }
    }

    public static function addpinglun($param)
    {
        $map = [];

        $collection = 'live_user_msg_' . $param['liveId'];

        //计算发送时间
        // 创建DateTime对象
        $dateTime = new DateTime($param['send_time']);
        // 转换为UTC时间
        $dateTime->setTimezone(new DateTimeZone('UTC'));
        // 输出结果
        $send_time2 = date('H:i:s', strtotime($dateTime->format('Y-m-d H:i:s')) + 8 * 60 * 60);

        $map['wx_avatarurl'] = $param['wx_avatarurl'];
        $map['nickname'] = $param['nickname'];
        $map['content'] = $param['content'];
        $map['send_time'] = $param['send_time'];
        $map['send_time2'] = $send_time2;
        $map['live_user_id'] = self::createShuziren($param);
        $map['create_time'] = date('Y-m-d H:i:s', time());
        $map['user_type'] = 'r';
        $map['del'] = 0;
        $map['is_real_msg'] = false;
        $data = ActionModel::addpinglun($collection, $map);

        if ($data) {
            return Res::ok('删除成功');
        } else {
            return Res::error('删除失败');
        }
    }

    //创建数字人  注册im的ID  获取数字人userid
    public static function createShuziren($param, $flagid = 1)
    {

        $nick = $param['nickname']; //每页显示条数
        $pinglun = $param['content']; //每页显示条数
        // $flagid = $param['user_type']; //每页显示条数
        $touxiang_img = $param['wx_avatarurl']; //每页显示条数

        if (!$flagid || $flagid == '') {
            return Res::error('类型不能为空');
        }
        if (!$pinglun || $pinglun == '') {
            return Res::error('评论不能为空');
        }

        $data = array();
        $data['nick'] = $nick;
        $data['pinglun'] = $pinglun;
        $data['flagid'] = $flagid;
        $data['touxiang_img'] = $touxiang_img;
        $data['createtime'] = date('Y-m-d H:i:s');

        $imService = new TImService();
        $insertId = null;
        Db::startTrans();
        try {

            $insertId = DB::table('xh_siyu_shuziren_list')->insert($data, true);
            $insertId = "R_" .env("Live.live_pre") . $insertId;
            // 添加账号
            $ret = $imService->account_import_one($insertId, $data["nick"], $data["touxiang_img"]);
            if ($ret !== true) {
                throw new \Exception("导入账号失败");
            }
            Db::commit();

            return $insertId;

        } catch (\Throwable $th) {

            if (empty($insertId)) {
                $imService->account_delete_ids([$insertId]);
            }
            Db::rollback();
            // throw $th;

            return Res::error($th->getMessage());

        }

    }


    public static function editpinglun($param)
    {
        $map = [];

        $collection = 'live_user_msg_' . $param['liveId'];

        //计算发送时间
        // 创建DateTime对象
        $dateTime = new DateTime($param['send_time']);
        // 转换为UTC时间
        $dateTime->setTimezone(new DateTimeZone('UTC'));
        // 输出结果
        $send_time2 = date('H:i:s', strtotime($dateTime->format('Y-m-d H:i:s')) + 8 * 60 * 60);

        $map['_id'] = new ObjectID($param['_id']['$oid']);
        $update = ['$set' => ['content' => $param['content'], 'send_time' => $param['send_time'], 'send_time2' => $send_time2]]; // 定义更新操作
        $data = ActionModel::editpinglun($collection, $map, $update);

        if ($data) {
            return Res::ok('保存成功');
        } else {
            return Res::error('保存失败');
        }
    }

    //管控记录
    public static function getguankong($param)
    {

        $limit = !isset($param['pageSize']) ? 10 : (int) $param['pageSize'];
        $offset = ($param['pageNumber'] - 1) * $limit;

        $map = [];
        $map['del'] = 0;
        $map['liveId'] = $param['liveId'];
        if (isset($param["input"]) && !empty($param["input"])) {
            if (isset($param["typeinput"]) && !empty($param["typeinput"])) {
                switch ($param["typeinput"]) {
                    case 1:  //用户昵称
                        // 构建查询
                        $map['user_name'] = ['$regex' => $param['input'], '$options' => 'i'];
                        break;
                    case 2:  //操作人昵称
                        // 构建查询
                        $map['operator_name'] = ['$regex' => $param['input'], '$options' => 'i'];
                        break;
                    default:
                        # code...
                        break;
                }
            }
        }

        if (isset($param["guankong_leixing"]) && !empty($param["guankong_leixing"])) {
            $map["operator_type"] = $param["guankong_leixing"];
        }

        $findName = [];

        $collection = 'live_control';

        $result = ActionModel::pageMap_guankong($collection, $map, $findName, $offset, $limit);
        $total = ActionModel::pageMapCount_guankong($collection, $map);

        foreach ($result as $k => $v) {
            $result[$k]['time'] = date('Y-m-d H:i:s', $v['operator_time']);
            switch ($v['operator_type']) {
                case 1:
                    $result[$k]['operator_event'] = '隐藏';
                    break;
                case 2:
                    $result[$k]['operator_event'] = '禁言';
                    break;
                case 3:
                    $result[$k]['operator_event'] = '解除禁言';
                    break;
                case 4:
                    $result[$k]['operator_event'] = '显示';
                    break;
                case 5:
                    $result[$k]['operator_event'] = '关闭直播';
                    break;

                default:
                    # code...
                    break;
            }

        }

        $data = [
            'list' => $result,
            'total' => $total,
        ];
        return Res::ok($data);
    }

    public static function pldelguankong($param)
    {
        $map = [];

        $ids = [];
        foreach ($param['ids'] as $k => $v) {
            $ids[] = new ObjectID($v['$oid']);
        }

        $collection = 'live_control';

        $map['_id'] = ['$in' => $ids];
        $update = ['$set' => ['del' => 1]]; // 定义更新操作
        $data = ActionModel::pldelUserInfo($collection, $map, $update);

        if ($data) {
            return Res::ok('删除成功');
        } else {
            return Res::error('删除失败');
        }
    }

    public static function delguankong($param)
    {
        $map = [];

        $collection = 'live_control';

        $map['_id'] = new ObjectID($param['id']['$oid']);
        $update = ['$set' => ['del' => 1]]; // 定义更新操作
        $data = ActionModel::delUserInfo($collection, $map, $update);

        if ($data) {
            return Res::ok('删除成功');
        } else {
            return Res::error('删除失败');
        }
    }

    // //邀约到课
    // public static function getyaoyue($param)
    // {
    //     $limit = !isset($param['pageSize']) ? 10 : (int) $param['pageSize'];
    //     $offset = ($param['pageNumber'] - 1) * $limit;

    //     $map = [
    //         'live_id' => '' . $param['liveId']
    //     ];

    //     $findName = [ // 投影需要的字段
    //         '_id' => 1,
    //         'tutor_id' => 1,
    //         'tutor_nickname' => 1,
    //         'yaoyuenum' => 1,   //邀请用户数
    //         'daokenum' => 1,   //到课用户数
    //         'yaoyue_daoke_lv' => 1,   //邀请到课率
    //     ];
    //     $collection = 'live_invitation';
    //     $data = [];
    //     $result = ActionModel::pageMap_yaoyue($collection, $map, $findName, $offset, $limit);
    //     $total = ActionModel::pageMapCount_yaoyue($collection, $map);

    //     $data = [
    //         'list' => $result,
    //         'total' => $total,
    //     ];
    //     return Res::ok($data);
    // }


    /**
     * @author: zwx
     * @Description: 邀约   mysql库取数
     * @param {*} $param
     * @Date: 2024-07-03 23:13:13
     */
    public static function getyaoyue($param)
    {
        $where = [];
        $where[] = ['del', '=', 0];
        $where[] = ['liveId', '=', $param['liveId']];
        if ($param['roleId'] == 11) {//规划师
            $where[] = ['sender', '=', $param['userName']];
        }elseif($param['roleId']==12){//销售主管账号
            $sql="SELECT userid FROM xh_zhujiao_group_list WHERE groupid IN (

                    SELECT id FROM xh_zhujiao_group WHERE managerid IN (
                    
                        SELECT id FROM xh_manager WHERE account = ?
                    )

	        )";
            $result = Db::query($sql,[$param['userName']]);
            if(!$result){
                $data = [
                    'sender' => [],
                    'yaoyue' => [],
                    'daoke' => [],
                    'list' => [],
                    'total' => 0
                ];
                return Res::ok($data);
            }
            $userids = array_column($result,'userid');
            $where[] = ['sender','in',$userids]; 
        }
        $limit = !isset($param['pageSize']) ? 10 : (int) $param['pageSize'];
        $offset = ($param['pageNumber'] - 1) * $limit;

        //查询邀约人
        $yaoyue = db::table('xh_siyu_invitation_user')->where('invitation_status', 2)->where($where)->field('sender,wx_unionid')->select();
        //查询到课人
        $daokes = db::table('xh_siyu_invitation_user')->where('signin_status', 2)->where($where)->field('sender,wx_unionid')->select();

        //查询邀约人信息
        $account = $account1 = [];
        if (!empty($yaoyue)) {
            $account = array_unique(array_column($yaoyue->toArray(), 'sender'));
        }
        if (!empty($daokes)) {
            $account1 = array_unique(array_column($daokes->toArray(), 'sender'));
        }
        $yaoyue1 = [];
        foreach ($account as $v) {
            $yaoyue1[] = [
                'sender' => $v,
                'yaoyuenum' => db::table('xh_siyu_invitation_user')->where('invitation_status', 2)->where($where)->where(['sender' => $v])->group('wx_unionid')->count(),
            ];
        }
        $daoke = [];
        foreach ($account1 as $v1) {
            $daoke[] = [
                'sender' => $v1,
                'daokenum' => db::table('xh_siyu_invitation_user')->where('signin_status', 2)->where($where)->where(['sender' => $v1])->group('wx_unionid')->count(),
            ];
        }
        $sender = db::table('xh_siyu_sys_member')->where('user_name', 'in', $account)->field('uid,user_name,nick_name')->select();

        //组合数据
        $zyaoyue = 0;
        $zdaoke = 0;
        $zlv = 0;
        foreach ($yaoyue1 as $k => $v) {
            $zyaoyue = $zyaoyue + $v['yaoyuenum'];
            $yaoyue1[$k]['id'] = $k + 1;
            $yaoyue1[$k]['daokenum'] = 0;
            $yaoyue1[$k]['tutor_nickname'] = '-';
            $yaoyue1[$k]['tutor_id'] = 0;
            $yaoyue1[$k]['yaoyue_daoke_lv'] = '0%';
            foreach ($daoke as $k2 => $v2) {
                //到课数
                if ($v['sender'] == $v2['sender']) {
                    $yaoyue1[$k]['daokenum'] = $v2['daokenum'];
                    $zdaoke = $zdaoke + $v2['daokenum'];
                    if ($v['yaoyuenum']) {
                        $yaoyue1[$k]['yaoyue_daoke_lv'] = number_format($v2['daokenum'] * 100 / $v['yaoyuenum'], 2) . '%';
                    }
                }
            }
            //邀约人
            foreach ($sender as $k3 => $v3) {
                if ($v['sender'] == $v3['user_name']) {
                    $yaoyue1[$k]['tutor_id'] = $v3['uid'];
                    $yaoyue1[$k]['tutor_nickname'] = $v3['nick_name'];
                }
            }
        }

        if ($zyaoyue) {
            $zlv = number_format($zdaoke * 100 / $zyaoyue, 2) . '%';
        } else {
            $zlv = '0%';
        }

        $total = count($yaoyue1);
        $yaoyue1[$total]['sender'] = '总计';
        $yaoyue1[$total]['yaoyuenum'] = $zyaoyue;
        $yaoyue1[$total]['daokenum'] = $zdaoke;
        $yaoyue1[$total]['yaoyue_daoke_lv'] = $zlv;
        $yaoyue1[$total]['tutor_nickname'] = '总计';

        $data = [
            'sender' => $sender,
            'yaoyue' => $yaoyue,
            'daoke' => $daoke,
            'list' => $yaoyue1,
            'total' => $total,
        ];
        return Res::ok($data);
    }


    //营销操作

    public static function getyingxiao($param)
    {
        $limit = !isset($param['pageSize']) ? 10 : (int) $param['pageSize'];
        $offset = ($param['pageNumber'] - 1) * $limit;

        $map = [];
        $map['del'] = 0;
        $map['liveId'] = '' . $param['liveId'];
        if (isset($param["input"]) && !empty($param["input"])) {
            if (isset($param["typeinput"]) && !empty($param["typeinput"])) {
                switch ($param["typeinput"]) {
                    case 1:  //用户昵称
                        // 构建查询
                        $map['operator_name'] = ['$regex' => $param['input'], '$options' => 'i'];
                        break;
                    case 2:  //用户手机号
                        // 构建查询
                        $map['shuziren_name'] = ['$regex' => $param['input'], '$options' => 'i'];
                        break;
                    default:
                        # code...
                        break;
                }
            }
        }

        if (isset($param["caozuo_leixing"]) && !empty($param["caozuo_leixing"])) {
            $map["operator_type"] = intval($param["caozuo_leixing"]);
        }

        $findName = [];

        $collection = 'live_marketing';

        $result = ActionModel::pageMap_yingxiao($collection, $map, $findName, $offset, $limit);
        $total = ActionModel::pageMapCount_yingxiao($collection, $map);

        foreach ($result as $k => $v) {

            switch ($v['operator_type']) {
                case 1:
                    $result[$k]['operator_content'] = '数字人进场';
                    break;
                case 2:
                    $result[$k]['operator_content'] = '数字人下单';
                    break;
                case 3:
                    $result[$k]['operator_content'] = '浏览小黄车';
                    break;
                case 4:
                    $result[$k]['operator_content'] = '数字人评论';
                    break;
                case 5:
                    $result[$k]['operator_content'] = '显示弹卡';
                    break;
                case 6:
                    $result[$k]['operator_content'] = '隐藏弹卡';
                    break;
                case 7:
                    $result[$k]['operator_content'] = '热卖飘字';
                    break;
                case 8:
                    $result[$k]['operator_content'] = '库存飘字';
                    break;
                case 9:
                    $result[$k]['operator_content'] = '隐藏昵称';
                    break;
                case 10:
                    $result[$k]['operator_content'] = '显示昵称';
                    break;
                case 11:
                    $result[$k]['operator_content'] = '显示在线人数';
                    break;
                case 12:
                    $result[$k]['operator_content'] = '显示小黄车';
                    break;
                case 13:
                    $result[$k]['operator_content'] = '隐藏小黄车';
                    break;
                case 14:
                    $result[$k]['operator_content'] = '手动水军';
                    break;
                case 15:
                    $result[$k]['operator_content'] = '显示售空';
                    break;
                case 16:
                    $result[$k]['operator_content'] = '隐藏售空';
                    break;
                case 17:
                    $result[$k]['operator_content'] = '发起抽奖';
                    break;
                case 18:
                    $result[$k]['operator_content'] = '抽奖开奖';
                    break;
                case 19:
                    $result[$k]['operator_content'] = '结束抽奖';
                    break;
                case 20:
                    $result[$k]['operator_content'] = '商品弹框';
                    break;

                default:
                    # code...
                    break;
            }

        }

        foreach ($result as $k => $v) {
            $result[$k]['time'] = date('Y-m-d H:i:s', $v['operator_time']);

        }


        $data = [
            'list' => $result,
            'total' => $total,
        ];
        return Res::ok($data);
    }

    public static function pldelyingxiao($param)
    {
        $map = [];

        $ids = [];
        foreach ($param['ids'] as $k => $v) {
            $ids[] = new ObjectID($v['$oid']);
        }

        $collection = 'live_marketing';

        $map['_id'] = ['$in' => $ids];
        $update = ['$set' => ['del' => 1]]; // 定义更新操作
        $data = ActionModel::pldelUserInfo($collection, $map, $update);

        if ($data) {
            return Res::ok('删除成功');
        } else {
            return Res::error('删除失败');
        }
    }

    public static function delyingxiao($param)
    {
        $map = [];

        $collection = 'live_marketing';

        $map['_id'] = new ObjectID($param['id']['$oid']);
        $update = ['$set' => ['del' => 1]]; // 定义更新操作
        $data = ActionModel::delUserInfo($collection, $map, $update);

        if ($data) {
            return Res::ok('删除成功');
        } else {
            return Res::error('删除失败');
        }
    }

    //点击事件

    public static function getShijian($param)
    {
        $limit = !isset($param['pageSize']) ? 10 : (int) $param['pageSize'];
        $offset = ($param['pageNumber'] - 1) * $limit;

        $map = [];
        $map["del"] = 0;
        $map["liveId"] = $param['liveId'];
        $map["user_id"] = $param['user_id'];

        if (isset($param["input"]) && !empty($param["input"])) {
            $map['coursename'] = ['$regex' => $param['input'], '$options' => 'i'];
        }

        if (isset($param["type"]) && !empty($param["type"])) {
            $map["type"] = intval($param["type"]);
        }

        $findName = [];

        $collection = 'live_event';

        $result = ActionModel::pageMap_shijian($collection, $map, $findName, $offset, $limit);
        $total = ActionModel::pageMapCount_shijian($collection, $map);

        foreach ($result as $k => $v) {
            if ($v['type'] == 1) {
                $result[$k]['typename'] = '小黄车内点击';
            } else if ($v['type'] == 2) {
                $result[$k]['typename'] = '右下角课程点击';
            } else if ($v['type'] == 20) {
                $result[$k]['typename'] = '商品弹窗点击';
            }

            $result[$k]['dianji_time'] = date('Y-m-d H:i:s', $v['create_time']);

        }
        $data = [
            'list' => $result,
            'total' => $total,
        ];
        return Res::ok($data);
    }
    public static function getChouJiang($param)
    {
        $liveId = $param['liveId'];

        $result = ChouJiangModel::list($liveId);
        $total = ChouJiangModel::total($liveId);
        $data = [
            'list' => $result,
            'total' => $total,
        ];
        return Res::ok($data);
    }
    public static function getPrizeList($param)
    {
        $result = PrizeUserModel::list($param);
        $total = PrizeUserModel::total($param);

        $data = [
            'list' => $result,
            'total' => $total,
        ];
        return Res::ok($data);
    }
    public static function getChouJiangUser($param)
    {
        $result=[];
        if(!isset($param['liveId']) && empty($param['liveId'])){
            return Res::ok($result);
        }
        $liveId=$param['liveId'];
        $user_list = \app\apiadmin\model\behavior\LiveUserMongo::user_list($liveId);
        if(!$user_list){
            return Res::ok($result);
        }
        foreach($user_list as $item){
            $value=[
                'id'=>$item->live_user_id,
                'label'=>$item->nickname,
                'pinyin'=>$item->nickname
            ];
            $result[]=$value;
        }

        return Res::ok($result);

    }
    public static function getChouJiangRobot()
    {
        $result = ShuZiRenModel::list();

        return Res::ok($result);
    }
    public static function getAllChouJiangUser($param)
    {
        $result = UserModel::getAllChouJiangUser($param);
        return Res::ok($result);
    }
    public static function getAllChouJiangRobot($param)
    {
        $result = ShuZiRenModel::getAllChouJiangRobot($param);
        return Res::ok($result);
    }
    public static function getEditChouJiangUser($param)
    {
        $result = UserModel::getEditChouJiangUser($param);
        return Res::ok($result);
    }
    public static function getEditChouJiangRobot($param)
    {
        $result = ShuZiRenModel::getEditChouJiangRobot($param);
        return Res::ok($result);
    }
    public static function getfinish($param)
    {
        $id = $param['id'];
        $result = ChouJiangModel::getfinish($id);
        return Res::ok($result);
    }


    //在线记录

    public static function getjilu($param)
    {
        $limit = !isset($param['pageSize']) ? 10 : (int) $param['pageSize'];
        $offset = ($param['pageNumber'] - 1) * $limit;

        $map = [];
        $map["del"] = "0";
        $map["live_user_id"] = $param['user_id'];



        $findName = [];

        $collection = 'live_user_' . $param['liveId'];

        $result = ActionModel::pageMap_jilu($collection, $map, $findName);
        // $total = ActionModel::pageMapCount_jilu($collection,$map);

        //开播时间
        $redis = new RedisService();
        $kaibotime = $redis->getLiveStartTime($param['liveId']);
        if ($kaibotime) {
            $kaibotime = db::table('xh_siyu_live_manager')->where(['id' => $param['liveId']])->value('service_start_time');
        }

        //整理数据
        $result2 = [];
        $total = 0;
        foreach ($result as $k => $v) {
            if ($v['enter_log']) {
                foreach ($v['enter_log'] as $k2 => $v2) {
                    if ($v2['status'] == 1) {
                        if (count($result2) > 0 && !isset($result2[count($result2) - 1]['enter_time'])) {
                            $result2[count($result2) - 1]['enter_time'] = !empty($v2['event_time']) ? gmdate('H:i:s', $v2['event_time']->toDateTime()->getTimestamp() - $kaibotime) : 0;
                        } else {
                            $a = [];
                            $a['enter_time'] = !empty($v2['event_time']) ? gmdate('H:i:s', $v2['event_time']->toDateTime()->getTimestamp() - $kaibotime) : 0;
                            $result2[] = $a;
                        }
                    } else if ($v2['status'] == 0) {
                        if (count($result2) > 0 && !isset($result2[count($result2) - 1]['event_time'])) {
                            $result2[count($result2) - 1]['exit_time'] = !empty($v2['event_time']) ? gmdate('H:i:s', $v2['event_time']->toDateTime()->getTimestamp() - $kaibotime) : 0;
                        } else {
                            $a = [];
                            $a['exit_time'] = !empty($v2['event_time']) ? gmdate('H:i:s', $v2['event_time']->toDateTime()->getTimestamp() - $kaibotime) : 0;
                            $result2[] = $a;
                        }
                    }
                }
            }
        }

        //组合数据
        $result3 = [];
        foreach ($result2 as $k => $v) {
            if ($k >= $offset && $k < $offset + $limit) {
                $result2[$k]['id'] = $k + 1;
                if (isset($v['exit_time']) && isset($v['enter_time'])) {
                    $result2[$k]['online_time'] = gmdate('H:i:s', strtotime($v['exit_time']) - strtotime($v['enter_time']));
                } else {
                    $result2[$k]['online_time'] = 0;
                }
                $result2[$k]['id'] = $k+1;
                $result3[] = $result2[$k];
            }

        }

        $data = [
            'list' => $result3,
            'total' => count($result2)
        ];
        return Res::ok($data);
    }


    // //课程购买记录

    // public static function getpayorder($param)
    // {
    //     $limit = !isset($param['pageSize']) ? 10 : (int) $param['pageSize'];
    //     $offset = ($param['pageNumber'] - 1) * $limit;

    //     $map = [];
    //     $map["del"] = 0;
    //     $map["liveId"] = $param['liveId'];
    //     // $map["user_id"] = $param['user_id'];
    //     if (isset($param["user_id"]) && !empty($param["user_id"])) {
    //         $map["user_id"] = $param["user_id"];
    //     }

    //     if (isset($param["input"]) && !empty($param["input"])) {
    //         $map['coursename'] = ['$regex' => $param['input'], '$options' => 'i'];
    //     }

    //     if (isset($param["fliter"]) && !empty($param["fliter"])) {
    //         $map["state"] = intval($param["fliter"]);
    //     }

    //     if (isset($param["state"]) && !empty($param["state"]) || (isset($param["state"]) && $param["state"] === "0")) {
    //         $map["state"] = intval($param["state"]);
    //     }

    //     $findName = [];

    //     $collection = 'live_order';

    //     $result = ActionModel::pageMap_payorder($collection, $map, $findName, $offset, $limit);
    //     $total = ActionModel::pageMapCount_payorder($collection, $map);

    //     foreach ($result as $k => $v) {
    //         if ($v['state'] == 0) {
    //             $result[$k]['statename'] = '提单未付款';
    //         } else if ($v['state'] == 1) {
    //             $result[$k]['statename'] = '下单';
    //         } else if ($v['state'] == 2) {
    //             $result[$k]['statename'] = '退款中';
    //         } else if ($v['state'] == 3) {
    //             $result[$k]['statename'] = '已退款';
    //         } else if ($v['state'] == 4) {
    //             $result[$k]['statename'] = '已失效';
    //         }

    //     }

    //     $data = [
    //         'map' => $map,
    //         'list' => $result,
    //         'total' => $total,
    //     ];
    //     return Res::ok($data);
    // }


    //课程购买记录

    public static function getpayorder($param)
    {
        $limit = !isset($param['pageSize']) ? 10 : (int) $param['pageSize'];
        $offset = ($param['pageNumber'] - 1) * $limit;

        $map = [];
        $map["o.del"] = 0;
        $map["o.liveId"] = $param['liveId'];

        if (isset($param["input"]) && !empty($param["input"])) {
            $map[] = ['c.name', 'like', '%' . $param["input"] . '%'];
        }

        if (isset($param["user_id"]) && !empty($param["user_id"])) {
            $map["o.user_id"] = $param['user_id'];
        }



        if (isset($param["state"]) && !empty($param["state"]) || (isset($param["state"]) && $param["state"] === "0")) {
            $map["o.state"] = intval($param["state"]);
        }


        $result = db::table('xh_siyu_order')
            ->field('o.*,c.name as coursename')
            ->alias('o')
            ->join(['xh_siyu_course' => 'c'], 'o.courseid=c.id', 'left')
            ->limit($offset, $limit)
            ->where($map)->select()->toArray();


        $total = db::table('xh_siyu_order')
            ->alias('o')
            ->join(['xh_siyu_course' => 'c'], 'o.courseid=c.id', 'left')
            ->where($map)->count();

        foreach ($result as $k => $v) {
            $result[$k]['fee'] = number_format($v['fee'] / 100, 2);
            if ($v['state'] == 0) {
                $result[$k]['statename'] = '提单未付款';
            } else if ($v['state'] == 1) {
                $result[$k]['statename'] = '下单';
            } else if ($v['state'] == 2) {
                $result[$k]['statename'] = '退款审核中';
            } else if ($v['state'] == 3) {
                $result[$k]['statename'] = '已退款';
            } else if ($v['state'] == 4) {
                $result[$k]['statename'] = '退款失败';
            } else if ($v['state'] == 5) {
                $result[$k]['statename'] = '已失效';
            }

        }

        $data = [
            'map' => $map,
            'list' => $result,
            'total' => $total,
        ];
        return Res::ok($data);
    }


    //课程购买记录 中 评论

    public static function getPingLunUser($param)
    {

        $limit = !isset($param['pageSize']) ? 10 : (int) $param['pageSize'];
        $offset = ($param['pageNumber'] - 1) * $limit;

        $map = [];
        $map["del"] = 0;
        $map["live_user_id"] = $param['user_id'];

        if (isset($param["input"]) && !empty($param["input"])) {
            $map['content'] = ['$regex' => $param['input'], '$options' => 'i'];
        }

        $findName = [];

        $collection = 'live_user_msg_' . $param['liveId'];

        $result = ActionModel::pageMap_pinglunuser($collection, $map, $findName, $offset, $limit);
        $total = ActionModel::pageMapCount_pinglunuser($collection, $map);

        foreach ($result as $k => $v) {
            $result[$k]['send_time'] = $v['create_time']->toDateTime()->format('Y-m-d H:i:s');
            if ($v['content_state'] == 1) {
                $result[$k]['content_state'] = '正常';
            } else if ($v['content_state'] == 2) {
                $result[$k]['content_state'] = '禁言';
            }
        }

        $data = [
            'list' => $result,
            'total' => $total,
        ];
        return Res::ok($data);
    }
    /*
     *  公告日志记录
     * */
    public static function getNoticeList($param)
    {

        $limit = !isset($param['pageSize']) ? 10 : (int) $param['pageSize'];
        $offset = ($param['pageNumber'] - 1) * $limit;

        $map = [];
        $map["liveId"] = $param['liveId'];


        if (isset($param["input"]) && !empty($param["input"])) {
            switch ($param["typeinput"]) {
                case 1:  //发布者昵称
                    $map['tutor_name'] = ['$regex' => $param['input'], '$options' => 'i'];
                    break;
                case 2:  //发布内容
                    $map['txt'] = ['$regex' => $param['input'], '$options' => 'i'];
                    break;
                default:
                    # code...
                    break;
            }
        }

        $findName = [];
        $collection = 'live_notice';

        $result = ActionModel::pageMap_pinglunuser($collection, $map, $findName, $offset, $limit);
        $total = ActionModel::pageMapCount_pinglunuser($collection, $map);

        foreach ($result as $k => $v) {
            $result[$k]['send_time'] = getmTime($v['send_time']);
        }

        $data = [
            'list' => $result,
            'total' => $total,
        ];
        return Res::ok($data);
    }
    /*
     *  小黑板日志记录
     * */
    public static function getBlackbordList($param)
    {

        $limit = !isset($param['pageSize']) ? 10 : (int) $param['pageSize'];
        $offset = ($param['pageNumber'] - 1) * $limit;

        $map = [];
        $map["liveId"] = $param['liveId'];

        if (isset($param["input"]) && !empty($param["input"])) {
            switch ($param["typeinput"]) {
                case 1:  //发布者昵称
                    $map['tutor_name'] = ['$regex' => $param['input'], '$options' => 'i'];
                    break;
                case 2:  //发布内容
                    $map['txt'] = ['$regex' => $param['input'], '$options' => 'i'];
                    break;
                default:
                    # code...
                    break;
            }
        }

        $findName = [];

        $collection = 'live_blackboard';

        $result = ActionModel::pageMap_pinglunuser($collection, $map, $findName, $offset, $limit);
        $total = ActionModel::pageMapCount_pinglunuser($collection, $map);
        foreach ($result as $k => $v) {
            $result[$k]['send_time'] = getmTime($v['send_time']);
        }

        $data = [
            'list' => $result,
            'total' => $total,
        ];
        return Res::ok($data);
    }

    /**
     * 邀约到课看板
     * @param array $params
     * @return \think\response\Json
     * @author cc
     */
    public static function getDaokekanban(array $params)
    {
        $where = [];
        $where[] = ['del', '=', 0];
        if (isset($params['roleId']) && $params['roleId'] == 11) {
            $where[] = ['sender', '=', $params['userName']];
        }
        if (isset($params['liveId']) && !empty($params['liveId'])){
            $where[] = ['liveId','=',$params['liveId']];
        }

        $invitationUser = db::table('xh_siyu_invitation_user')->where($where)->group('wx_unionid')->select();
        
        $yaoyue = $daoke = $chengjiao = 0;
        foreach ($invitationUser as $value) {
            if ($value['invitation_status'] == 2) {
                $yaoyue++;
            } 
            if ($value['signin_status'] == 2) {
                $daoke++;
            }
            if ($value['chengjiao_status'] == 2) {
                $chengjiao++;
            }
        }
        $daokelv = $yaoyue != 0 ? number_format($daoke * 100 / $yaoyue, 2) . '%' : '0.00%';
        $chengjiaolv = $daoke != 0 ? number_format($chengjiao * 100 / $daoke, 2) . '%' : '0.00%';
        $yaoyuelv = $yaoyue != 0 ? number_format($chengjiao * 100 / $yaoyue, 2) . '%' : '0.00%';

        $data = [
            'yaoyue' => $yaoyue,
            'daoke' => $daoke,
            'chengjiao' => $chengjiao,
            'daokelv' => $daokelv,
            'chengjiaolv' => $chengjiaolv,
            'yaoyuelv' => $yaoyuelv,
        ];
        return Res::ok($data);
    }

    /**
     * 邀约到课用户
     * @param array $params
     * @return \think\response\Json
     * @author cc
     */
    public static function getYaoyueDaokeUser2($param)
    {
        $limit = !isset($param['pageSize']) ? 10 : (int) $param['pageSize'];
        $offset = ($param['pageNumber'] - 1) * $limit;

        //查询用户信息
        $findName = [];
        $map = [];
        $map['del'] = "0";

        if(isset($param["online"]) && $param["online"] != ""){
            $map['online_status'] = ''.$param["online"];
        }
        $collection = 'live_user_' . $param['liveId'];
        $result = ActionModel::live_user_info($collection, $map, $findName);

        //查询邀请助教
        $userids = array_column($result, "live_user_id");
        $map1 = [];
        $map1["del"] = 0;
        $map1["liveId"] = ''.$param['liveId'];
        $map1['user_id'] = ['$in' => $userids];
        if (isset($param['weizhifu']) && $param["weizhifu"] != "") {
            $map1['event'] = '提单未付款';
        }
        $collection = 'live_event';
        $result1 = ActionModel::live_event($collection, $map1, $findName);
        Log::channel("liveInfo")->info("live_event:" . json_encode($result1));

        $map2 = [];
        $map2["del"] = 0;
        $map2["liveId"] = ''.$param['liveId'];
        $map2['user_id'] = ['$in' => $userids];
        if (isset($param["tutorId"]) && !empty($param["tutorId"])) {
            $map2['tutor_id'] = $param["tutorId"];
        }
        $collection = 'live_invitation';
        $result2 = ActionModel::getYaoqingZhujiao($collection, $map2);

        //查询接量助教
        $unionids = array_column($result, "wx_unionid");
        $map3 = array();
        $map3[] = array('a.union_id', 'in', $unionids);
        $findName = 'a.union_id,b.name,a.teacher_id';
        $result3 = ActionModel::getJieliangZhujiao($map3, $findName);

        $where = [];
        $where[] = ['del', '=', 0];
        $where[] = ['liveId', '=', $param['liveId']];
        $where[] = ['sender', '=', $param['sender']];

        $unionidArray = db::table('xh_siyu_invitation_user')->where($where)->column('wx_unionid');
        Log::channel("liveInfo")->info("wx_unionid:" . json_encode($unionidArray));
        $unionidArr = [];
        foreach ($unionidArray as $key => $value) {
            $unionidArr[$key] = $value;
            $loginLog = Db::table('xh_siyu_user_order_zhujiao_log')->where(['live_id' => $param['liveId'], 'xh_order_union_id' => $value])->find();
            if ($loginLog && $loginLog['user_phone'] != $loginLog['validate_phone']) {
                $unionidArr[$key] = $loginLog['user_union_id'];
            }
        }
        Log::channel("liveInfo")->info("unionid_arr:" . json_encode($unionidArr));

        $daokeNum = db::table('xh_siyu_invitation_user')->where($where)->where('signin_status', '>', 0)->group('wx_unionid')->count();
        $yichengjiaoNum = db::table('xh_siyu_invitation_user')->where($where)->where('chengjiao_status', '>', 0)->group('wx_unionid')->count();
        $totalNum = db::table('xh_siyu_invitation_user')->where($where)->group('wx_unionid')->count();
        if(isset($param["daoke"]) && $param["daoke"] != ''){
            if ($param["daoke"] == 1) {
                $where[] = ['signin_status', '>', 0];
            } else {
                $where[] = ['signin_status', '=', 0];
            }
        }
        if(isset($param["zhifu"]) && $param["zhifu"] != ''){
            if ($param["zhifu"] == 1) {
                $where[] = ['chengjiao_status', '>', 0];
            } else {
                $where[] = ['chengjiao_status', '=', 0];
            }
        }
        if (isset($param["online"]) && !empty($param["online"])) {
            $onlineArr = array_column($result, 'wx_unionid');
            $where[] = ['wx_unionid', 'in', $onlineArr];
        }
        if (isset($param['weizhifu']) && $param["weizhifu"] != "") {
            $weizhifuArray = array_column($result1, 'user_id');
            Log::channel("liveInfo")->info("unionid_arr2:" . json_encode($weizhifuArray));
            $weizhifuArr = [];
            foreach ($weizhifuArray as $value) {
                $loginLog = Db::table('xh_siyu_user_order_zhujiao_log')->where(['live_id' => $param['liveId'], 'user_id' => $value])->find();
                if ($loginLog && $loginLog['user_phone'] != $loginLog['validate_phone']) {
                    $weizhifu = $loginLog['xh_order_union_id'];
                }else {
                    $weizhifu = db::table('xh_siyu_user')->where('id', $value)->value('unionid');
                }
                $weizhifuArr[] = $weizhifu;
            }
            Log::channel("liveInfo")->info("unionid_arr22:" . json_encode($weizhifuArray));
            if ($param["weizhifu"] == 1) {
                $where[] = ['wx_unionid', 'in', $weizhifuArr];
            } else {
                $where[] = ['wx_unionid', 'not in', $weizhifuArr];
            }
        }

        $yaoyue = db::table('xh_siyu_invitation_user')->where($where)->group('wx_unionid');
        $total = db::table('xh_siyu_invitation_user')->where($where)->group('wx_unionid')->count();
//        if ($total == 0) {
//            return Res::ok();
//        }
        $yaoyue = $yaoyue->limit($offset, $limit);
        $yaoyueList = $yaoyue->select();
        
        $userIdArr = Db::table('xh_siyu_user')->where('unionid', 'in', $unionidArr)->column('id');
        $userIdArray = array_map(function($item) {
            return (string)$item;
        }, $userIdArr);
        Log::channel("liveInfo")->info("userArr:" . json_encode($userIdArr));
        
        $wheres = [
            'live_user_id' => ['$in' => $userIdArray],
            'del' => 0,
            'online_status' => 1
        ];
        $zaixianNum = ActionModel::pageMapCount_userinfo('live_user_' . $param['liveId'], $wheres);

        $wheres1 = [
            'user_id' => ['$in' => $userIdArray],
            'del' => 0,
            'liveId' => ''.$param['liveId'],
            'event' => '提单未付款'
        ];
        $weifukuanArr = ActionModel::pageMap_shijian('live_event', $wheres1, [], $offset, $limit);
        $weifukuanNum = count(array_unique(array_column($weifukuanArr, 'user_id')));

        $list = [];
        foreach ($yaoyueList as $key => $user) {
            $zhujiaoLog = Db::table('xh_siyu_user_order_zhujiao_log')->where(['live_id' => $param['liveId'], 'xh_order_union_id' => $user['wx_unionid']])->find();
            if ($zhujiaoLog && $zhujiaoLog['user_phone'] != $zhujiaoLog['validate_phone']) {
                $user['wx_unionid'] = $zhujiaoLog['user_union_id'];
            }
                
            $res = [];
            $zaixianShichang = 0;
            $yaoqingzhujiao = Db::table('xh_manager')->where('account', $user['sender'])->value('nick');
            $tutorId = $jieliangzhujiao = $typename = $shijian = $payorder = '-';
            foreach ($result as $k => $v) {
                if(isset($v['wx_unionid'])){
                    if ($user['wx_unionid'] == $v['wx_unionid']) {
                        if (isset($v['online_time']) && !empty($v['online_time'])) {
                            $zaixianShichang = gmdate("H:i:s", $v['online_time']);
                        }
                        foreach ($result2 as $k2 => $v2) {
                            if ($v['live_user_id'] == $v2['user_id']) {
                                $yaoqingzhujiao = $v2['tutor_nickname'];
                                $tutorId = $v2['tutor_id'];
                            }
                        }
                        foreach ($result3 as $k3 => $v3) {
                            if (isset($v['wx_unionid']) && ($v['wx_unionid'] == $v3['union_id'])) {
                                $jieliangzhujiao = $v3['name'];
                            }
                        }
                        foreach ($result1 as $k4 => $v4) {
                            if ($v4['user_id'] == $v['live_user_id']) {
                                if ($v4['type'] == 1) {
                                    $typename = '小黄车内点击-' . date('H:i:s', $v4['create_time']);
                                } else if ($v4['type'] == 2) {
                                    $typename = '右下角课程点击-' . date('H:i:s', $v4['create_time']);
                                } else if ($v4['type'] == 20) {
                                    $typename = '商品弹窗点击-' . date('H:i:s', $v4['create_time']);
                                }
    
                                if ($v4['event'] == '提单未付款') {
                                    $shijian = $v4['coursename'] . '-' . date('H:i:s', $v4['create_time']);
                                }
                                if ($v4['event'] == '下单') {
                                    $payorder = $v4['coursename'] . '-' . date('H:i:s', $v4['create_time']);
                                }
                            }
                        }
                        $res = $v;
                    }
                }
            }
            $list[] = [
                'id' => $key + 1,
                'nickname' => !empty($res) && isset($res['nickname']) ? $res['nickname'] : $user['nickname'],
                'wx_unionid' => !empty($res) && isset($res['wx_unionid']) ? $res['wx_unionid'] : $user['wx_unionid'],
                'wx_avatarurl' => !empty($res) && isset($res['wx_avatarurl']) ? $res['wx_avatarurl'] : $user['headimgurl'],
                'phone' => !empty($res) && isset($res['phone']) ? $res['phone'] : $user['phone'],
                'tutor_id' => $tutorId,
                'signin_status' => $user['signin_status'],
                'jieliangzhujiao' => $jieliangzhujiao,
                'enter_log' => !empty($res) && isset($res['enter_log']) ? $res['enter_log'] : [],
                'exit_first' => !empty($res) && isset($res['exit_first']) ? $res['exit_first'] : '',
                'last_enter_time' => !empty($res) && isset($res['last_enter_time']) ? $res['last_enter_time'] : [],
                'last_leave_time' => !empty($res) && isset($res['last_leave_time']) ? $res['last_leave_time'] : [],
                'live_user_id' => !empty($res) && isset($res['live_user_id']) ? $res['live_user_id'] : '',
                'online_status' => !empty($res) && isset($res['online_status']) ? $res['online_status'] : '',
                'online_time' => !empty($res) && isset($res['online_time']) ? $res['online_time'] : '',
                'payorder' => $payorder,
                'shijian' => $shijian,
                'typename' => $typename,
                'yaoqingzhujiao' => $yaoqingzhujiao,
                'zaixian_shichang' => $zaixianShichang,
                'del' => 0,
            ];
        }


        $data = [
            'list' => $list,
            'total' => $total,
            'yaoyueNum' => $totalNum,
            'daokeNum' => $daokeNum,
            'weidaokeNum' => $totalNum-$daokeNum,
            'zaixianNum' => $zaixianNum,
            'yichengjiaoNum' => $yichengjiaoNum,
            'weifukuanNum' => $weifukuanNum,
        ];
        return Res::ok($data);

    }



    /**
     * 邀约到课用户  修改版
     * @param array $params
     * @return \think\response\Json
     * @author cc
     */
    public static function getYaoyueDaokeUser($param)
    {
        $limit = !isset($param['pageSize']) ? 10 : (int) $param['pageSize'];
        $offset = ($param['pageNumber'] - 1) * $limit;

        //查询用户信息
        $findName = [];
        $map = [];
        $map['del'] = "0";

        if(isset($param["online"]) && $param["online"] != ""){
            $map['online_status'] = ''.$param["online"];
        }
        $collection = 'live_user_' . $param['liveId'];
        $result = ActionModel::live_user_info($collection, $map, $findName);

        //查询邀请助教
        $userids = array_column($result, "live_user_id");
        $map1 = [];
        $map1["del"] = 0;
        $map1["liveId"] = ''.$param['liveId'];
        $map1['user_id'] = ['$in' => $userids];
        if (isset($param['weizhifu']) && $param["weizhifu"] != "") {
            $map1['event'] = '提单未付款';
        }
        $collection = 'live_event';
        $result1 = ActionModel::live_event($collection, $map1, $findName);
        Log::channel("liveInfo")->info("live_event:" . json_encode($result1));

        $map2 = [];
        $map2["del"] = 0;
        $map2["liveId"] = ''.$param['liveId'];
        $map2['user_id'] = ['$in' => $userids];
        if (isset($param["tutorId"]) && !empty($param["tutorId"])) {
            $map2['tutor_id'] = $param["tutorId"];
        }
        $collection = 'live_invitation';
        $result2 = ActionModel::getYaoqingZhujiao($collection, $map2);

        //查询接量助教
        $unionids = array_column($result, "wx_unionid");
        $map3 = array();
        $map3[] = array('a.union_id', 'in', $unionids);
        $findName = 'a.union_id,b.name,a.teacher_id';
        $result3 = ActionModel::getJieliangZhujiao($map3, $findName);

        //汇总到课数、成交数、总数
        $daokeNum =$yichengjiaoNum =$totalNum = 0;
        $where = [];
        $where[] = ['del', '=', 0];
        $where[] = ['liveId', '=', $param['liveId']];
        $where[] = ['sender', '=', $param['sender']];

        $invitationusers = db::table('xh_siyu_invitation_user')->where($where)->group('wx_unionid')->select()->toArray();
        foreach ($invitationusers as $k => $v) {
            $totalNum++;
            if($v['signin_status']>0){
                $daokeNum++;
            }
            if($v['chengjiao_status']>0){
                $yichengjiaoNum++;
            }
        }
        //通过验证进入的邀约人
        $unionidArray = array_column($invitationusers ,'wx_unionid');
        Log::channel("liveInfo")->info("wx_unionid:" . json_encode($unionidArray));
        $unionidArr = [];
        // foreach ($unionidArray as $key => $value) {
        //     $unionidArr[$key] = $value;
        //     $loginLog = Db::table('xh_siyu_user_order_zhujiao_log')->where(['live_id' => $param['liveId'], 'xh_order_union_id' => $value])->find();
        //     if ($loginLog && $loginLog['user_phone'] != $loginLog['validate_phone']) {
        //         $unionidArr[$key] = $loginLog['user_union_id'];
        //     }
        // }
        $where2 = [];
        $where2['live_id'] = $param['liveId'] ;
        $where2[] = ['xh_order_union_id', 'in', $unionidArray];
        $loginLogs = Db::table('xh_siyu_user_order_zhujiao_log')->where($where2)->select()->toArray();
        $daoke_sql = Db::table('xh_siyu_user_order_zhujiao_log')->where($where2)->fetchSql()->select();
        foreach ($unionidArray as $k => $v) {
            $unionidArr[$k] = $v;
            foreach ($loginLogs as $key => $loginLog) {
                if ($loginLog && $loginLog['user_phone'] != $loginLog['validate_phone']) {
                    $unionidArr[$k] = $loginLog['user_union_id'];
                }
            }
        }
        
        Log::channel("liveInfo")->info("unionid_arr:" . json_encode($unionidArr));

        // $daokeNum = db::table('xh_siyu_invitation_user')->where($where)->where('signin_status', '>', 0)->group('wx_unionid')->count();
        // $yichengjiaoNum = db::table('xh_siyu_invitation_user')->where($where)->where('chengjiao_status', '>', 0)->group('wx_unionid')->count();
        // $totalNum = db::table('xh_siyu_invitation_user')->where($where)->group('wx_unionid')->count();

        if(isset($param["daoke"]) && $param["daoke"] != ''){
            if ($param["daoke"] == 1) {
                $where[] = ['signin_status', '>', 0];
            } else {
                $where[] = ['signin_status', '=', 0];
            }
        }
        if(isset($param["zhifu"]) && $param["zhifu"] != ''){
            if ($param["zhifu"] == 1) {
                $where[] = ['chengjiao_status', '>', 0];
            } else {
                $where[] = ['chengjiao_status', '=', 0];
            }
        }
        if (isset($param["online"]) && $param["online"] != "") {
            $onlineArr = array_column($result, 'wx_unionid');
            $where[] = ['wx_unionid', 'in', $onlineArr];
        }
        if (isset($param['weizhifu']) && $param["weizhifu"] != "") {
            $weizhifuArray = array_column($result1, 'user_id');
            Log::channel("liveInfo")->info("unionid_arr2:" . json_encode($weizhifuArray));
            $weizhifuArr = [];
            // foreach ($weizhifuArray as $value) {
            //     $loginLog = Db::table('xh_siyu_user_order_zhujiao_log')->where(['live_id' => $param['liveId'], 'user_id' => $value])->find();
            //     if ($loginLog && $loginLog['user_phone'] != $loginLog['validate_phone']) {
            //         $weizhifu = $loginLog['xh_order_union_id'];
            //     }else {
            //         $weizhifu = db::table('xh_siyu_user')->where('id', $value)->value('unionid');
            //     }
            //     $weizhifuArr[] = $weizhifu;
            // }
            $where2 = [];
            $where2['live_id'] = $param['liveId'] ;
            $where2[] = ['user_id', 'in', $weizhifuArray];
            $loginLogs = Db::table('xh_siyu_user_order_zhujiao_log')->where($where2)->select()->toArray();
            foreach ($loginLogs as $key => $loginLog) {
                if ($loginLog && $loginLog['user_phone'] != $loginLog['validate_phone']) {
                    $weizhifuArr[$key] = $loginLog['xh_order_union_id'];
                }else {
                    $weizhifuArr[$key] = db::table('xh_siyu_user')->where('id', $loginLog['user_id'])->value('unionid');;
                }
            }

            Log::channel("liveInfo")->info("unionid_arr22:" . json_encode($weizhifuArray));
            if ($param["weizhifu"] == 1) {
                $where[] = ['wx_unionid', 'in', $weizhifuArr];
            } else {
                $where[] = ['wx_unionid', 'not in', $weizhifuArr];
            }
        }

        $yaoyue = db::table('xh_siyu_invitation_user')->where($where)->group('wx_unionid');
        $total = db::table('xh_siyu_invitation_user')->where($where)->group('wx_unionid')->count();
//        if ($total == 0) {
//            return Res::ok();
//        }
        $yaoyue = $yaoyue->limit($offset, $limit);
        $yaoyueList = $yaoyue->select()->toArray();
        $sql = $yaoyue->fetchSql()->select();
        //到课人id
        $where2 = [];
        $where2[] =  ['unionid', 'in', $unionidArr];
        $userIdArr = Db::table('xh_siyu_user')->where($where2)->column('id');
        $userIdArr_sql = Db::table('xh_siyu_user')->where($where2)->fetchSql()->column('id');
        $userIdArray = array_map(function($item) {
            return (string)$item;
        }, $userIdArr);
        Log::channel("liveInfo")->info("userArr:" . json_encode($userIdArr));
        
        $wheres = [
            'live_user_id' => ['$in' => $userIdArray],
            'del' => 0,
            'online_status' => 1
        ];
        $zaixianNum = ActionModel::pageMapCount_userinfo('live_user_' . $param['liveId'], $wheres);

        $wheres1 = [
            'user_id' => ['$in' => $userIdArray],
            'del' => 0,
            'liveId' => ''.$param['liveId'],
            'event' => '提单未付款'
        ];
        $weifukuanArr = ActionModel::pageMap_shijian2('live_event', $wheres1, []);
        $weifukuanNum = count(array_unique(array_column($weifukuanArr, 'user_id')));

        $list = [];
        $yaoqingzhujiao = Db::table('xh_manager')->where('account', $param['sender'])->value('nick');
        $unionids = array_column($yaoyueList,'wx_unionid');
        $where2 = [];
        $where2[] = ['xh_order_union_id', 'in', $unionids];
        $where2['live_id'] =  $param['liveId'];
        $zhujiaoLogs = Db::table('xh_siyu_user_order_zhujiao_log')->where($where2)->select()->toArray();
        foreach ($yaoyueList as $key => $user) {
            // $zhujiaoLog = Db::table('xh_siyu_user_order_zhujiao_log')->where(['live_id' => $param['liveId'], 'xh_order_union_id' => $user['wx_unionid']])->find();
            // if ($zhujiaoLog && $zhujiaoLog['user_phone'] != $zhujiaoLog['validate_phone']) {
            //     $user['wx_unionid'] = $zhujiaoLog['user_union_id'];
            // }

            foreach ($zhujiaoLogs as $k2 => $zhujiaoLog) {
                if ($zhujiaoLog && $zhujiaoLog['user_phone'] != $zhujiaoLog['validate_phone'] && $zhujiaoLog['xh_order_union_id'] == $user['wx_unionid']) {
                        $user['wx_unionid'] = $zhujiaoLog['user_union_id'];
                    }
            }
                
            $res = [];
            $zaixianShichang = 0;
            $tutorId = $jieliangzhujiao = $typename = $shijian = $payorder = '-';
            foreach ($result as $k => $v) {
                if(isset($v['wx_unionid'])){
                    if ($user['wx_unionid'] == $v['wx_unionid']) {
                        if (isset($v['online_time']) && !empty($v['online_time'])) {
                            $zaixianShichang = gmdate("H:i:s", $v['online_time']);
                        }
                        foreach ($result2 as $k2 => $v2) {
                            if ($v['live_user_id'] == $v2['user_id']) {
                                $yaoqingzhujiao = $v2['tutor_nickname'];
                                $tutorId = $v2['tutor_id'];
                            }
                        }
                        foreach ($result3 as $k3 => $v3) {
                            if (isset($v['wx_unionid']) && ($v['wx_unionid'] == $v3['union_id'])) {
                                $jieliangzhujiao = $v3['name'];
                            }
                        }
                        foreach ($result1 as $k4 => $v4) {
                            if ($v4['user_id'] == $v['live_user_id']) {
                                if ($v4['type'] == 1) {
                                    $typename = '小黄车内点击-' . date('H:i:s', $v4['create_time']);
                                } else if ($v4['type'] == 2) {
                                    $typename = '右下角课程点击-' . date('H:i:s', $v4['create_time']);
                                } else if ($v4['type'] == 20) {
                                    $typename = '商品弹窗点击-' . date('H:i:s', $v4['create_time']);
                                }
    
                                if ($v4['event'] == '提单未付款') {
                                    $shijian = $v4['coursename'] . '-' . date('H:i:s', $v4['create_time']);
                                }
                                if ($v4['event'] == '下单') {
                                    $payorder = $v4['coursename'] . '-' . date('H:i:s', $v4['create_time']);
                                }
                            }
                        }
                        $res = $v;
                    }
                }
            }
            $list[] = [
                'id' => $key + 1,
                'nickname' => !empty($res) && isset($res['nickname']) ? $res['nickname'] : $user['nickname'],
                'wx_unionid' => !empty($res) && isset($res['wx_unionid']) ? $res['wx_unionid'] : $user['wx_unionid'],
                'wx_avatarurl' => !empty($res) && isset($res['wx_avatarurl']) ? $res['wx_avatarurl'] : $user['headimgurl'],
                'phone' => !empty($res) && isset($res['phone']) ? $res['phone'] : $user['phone'],
                'tutor_id' => $tutorId,
                'signin_status' => $user['signin_status'],
                'jieliangzhujiao' => $jieliangzhujiao,
                'enter_log' => !empty($res) && isset($res['enter_log']) ? $res['enter_log'] : [],
                'exit_first' => !empty($res) && isset($res['exit_first']) ? $res['exit_first'] : '',
                'last_enter_time' => !empty($res) && isset($res['last_enter_time']) ? $res['last_enter_time'] : [],
                'last_leave_time' => !empty($res) && isset($res['last_leave_time']) ? $res['last_leave_time'] : [],
                'live_user_id' => !empty($res) && isset($res['live_user_id']) ? $res['live_user_id'] : '',
                'online_status' => !empty($res) && isset($res['online_status']) ? $res['online_status'] : '',
                'online_time' => !empty($res) && isset($res['online_time']) ? $res['online_time'] : '',
                'payorder' => $payorder,
                'shijian' => $shijian,
                'typename' => $typename,
                'yaoqingzhujiao' => $yaoqingzhujiao,
                'zaixian_shichang' => $zaixianShichang,
                'del' => 0,
            ];
        }


        $data = [
            'list' => $list,
            'total' => $total,
            'yaoyueNum' => $totalNum,
            'daokeNum' => $daokeNum,
            'weidaokeNum' => $totalNum-$daokeNum,
            'zaixianNum' => $zaixianNum,
            'yichengjiaoNum' => $yichengjiaoNum,
            'weifukuanNum' => $weifukuanNum,
            'sql'=>$sql,
            'unionidArray'=>$unionidArray,
            'unionidArr'=>$unionidArr,
           '$userIdArr_sql'=> $userIdArr_sql,
           '$daoke_sql'=>$daoke_sql
        ];
        return Res::ok($data);

    }





}